<script setup>
import { findUserByRoleId } from '@/api/system/role.js'
import { Search, Refresh } from '@element-plus/icons-vue'
import { usePagination } from '@/hooks/usePagination'

const route = useRoute()

const loading = ref(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

const tableData = ref([])
const searchFormRef = ref()
const searchData = reactive({
  userName: '',
  realName: '',
})
const getTableData = () => {
  loading.value = true
  const { query } = route
  const param = {
    ...searchData,
    roleId: query.id
  }
  findUserByRoleId(param, {
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
  })
    .then((res) => {
      paginationData.total = parseInt(res.data.total)
      tableData.value = res.data.records
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <div shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="userName" label="用户账号">
          <el-input v-model="searchData.userName" placeholder="请输入用户账号" clearable/>
        </el-form-item>
        <el-form-item prop="realName" label="真实姓名">
          <el-input v-model="searchData.realName" placeholder="请输入真实姓名" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">

      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" max-height="64vh">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="userName" label="用户账号" align="center" />
          <el-table-column prop="realName" label="真实姓名" align="center" />
          <el-table-column prop="sysOrgManagerName" label="所属组织" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.status === 0" type="success" effect="plain">启用</el-tag>
              <el-tag v-else type="danger" effect="plain">停用</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex justify-end">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :small="true"
        />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.table-wrapper {
  margin-bottom: 20px;
}
</style>
